/**
 * Created by dllo on 16/10/18.
 */
//随机数
function rand(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}


//div拖拽
function drag(obj) {
    obj.onmousedown = function(ev) {
        var evObj = ev || window.event;
        //
        var disX = evObj.clientX - obj.offsetLeft;
        var disY = evObj.clientY - obj.offsetTop;
        //在down里写onmousemove
        document.onmousemove = function(ev) {
            var e = ev || window.event;
            //计算div定位的点
            var x = e.clientX - disX;
            var y = e.clientY - disY;
            obj.style.left = x + "px";
            obj.style.top = y + "px";
            if(x <= 0) {
                obj.style.left = 0 + "px";
            }
        }
        document.onmouseup = function() {
            document.onmousemove = "";
        }
    }
}



//移动端拖拽
function dragTou(obj) {
    document.addEventListener("touchstart", function(ev) {
        obj.style.display = "block";
        document.body.appendChild(div);
        var touches = ev.touches[0];
        var x = touches.clientX - obj.offsetWidth / 2;
        var y = touches.clientY - obj.offsetHeight / 2;
        obj.style.left = x + "px";
        obj.style.top = y + "px";
        document.addEventListener("touchmove", function(e) {
            var touches = e.touches[0];
            var x = touches.clientX - obj.offsetWidth / 2;
            var y = touches.clientY - obj.offsetHeight / 2;
            obj.style.left = x + "px";
            obj.style.top = y + "px";
        }, false);
        event.preventDefault();
    }, false);
    document.addEventListener("touchend", function() {
        obj.style.display = "none";
    }, false);
}



//取消事件冒泡
function maoPao(obj) {
    obj.onclick = function(ev) {
        var evObj = ev || window.event;
        //阻止继续向父级传递事件
        //这种方式对火狐无效
        //evObj.cancelBubble = true;
        //stopPropagation()更通用一些
        evObj.stopPropagation();
    }
}


//碰撞函数（矩形）
function rectCrash(obj1, obj2) {
    var t1 = obj1.offsetTop;
    var b1 = obj1.offsetHeight + t1;
    var l1 = obj1.offsetLeft;
    var r1 = obj1.offsetWidth + l1;

    var t2 = obj2.offsetTop;
    var b2 = obj2.offsetHeight + t2;
    var l2 = obj2.offsetLeft;
    var r2 = obj2.offsetWidth + l2;

    if(b1 < t2 || t1 > b2 || r1 < l2 || l1 > r2) {
        return false;
    } else {
        return true;
    }
}

//碰撞（圆）
function rectCrash(obj1, obj2) {
    var t1 = obj1.offsetTop;
    var l1 = obj1.offsetLeft;
    var r1 = obj1.offsetWidth/2;

    var t2 = obj2.offsetTop;
    var l2 = obj2.offsetLeft;
    var r2 = obj2.offsetWidth/2;

    if((t1-t2)*(t1-t2)+(l1-l2)*(l1-l2)>(r1+r2)*(r1+r2)){
        return false;
    }else{
        return true;
    }
}




//滚轮事件
function mouseWheel(obj, fn) {
    var ff = window.navigator.userAgent.indexOf("Firefox");
    if(ff != -1) {
        obj.addEventListener("DOMMouseScroll", wheel, false);
    } else {
        obj.onmousewheel = wheel;
    }

    function wheel(ev) {
        var ev = ev || window.event;
        var down = true;
        if(ev.detail) {
            down = ev.detail < 0;
        } else {
            down = ev.wheelDelta > 0;
        }
        //回调函数，把值回传给调用处
        fn(down, ev);
        //取消默认事件
        if(ev.preventDefault) {
            ev.preventDefault();
        } else {
            return false;
        }
    }
}